//
//  LifeServiceCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/5.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "LifeServiceCell.h"

@interface LifeServiceCell ()
@property (weak, nonatomic) IBOutlet UIView *bigBgView;
@property (weak, nonatomic) IBOutlet UIView *topBgView;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet UILabel *yueDuLabel;

@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UILabel *bottomLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *topImageView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMargin;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewH;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMargFirst;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleTopMargin;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *yuDuMarginH;

@end

@implementation LifeServiceCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.yuDuMarginH.constant = 44;
    self.titleTopMargin.constant = 10;
    self.bgView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.bgView.layer.borderWidth = 0.6;
    self.bgView.layer.cornerRadius = 5;
    self.bgView.layer.masksToBounds = YES;
    
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bigBgView.backgroundColor = viewBgGrey;
    self.topBgView.backgroundColor = viewBgGrey;
   
    self.timeLabel.textColor = viewBgGrey;
    self.timeLabel.textColor = darkGray;
    
    self.titleLabel.font = MYFONT(17);
    self.bottomLabel.font = MYFONT(15);
    self.yueDuLabel.font = MYFONT(14);
    self.timeLabel.font = MYFONT(14);
    
    self.bottomLabel.textColor = darkGray;
    self.lineView.backgroundColor = lineGray;
    self.bottomMargFirst.constant = 0;
    self.bottomMargin.constant = 0;
    
    
    self.titleLabel.text = @"";
    
    self.bottomLabel.text = @"";
    
//    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:@"http://img1.3lian.com/2015/a1/88/d/282.jpg"] placeholderImage:nil];
    // Initialization code
}

- (void)setModel:(LifeServiceModel *)model{
    _model = model;
    
    self.titleLabel.text = _model.title;
    
    self.bottomLabel.text = _model.short_title;
    
    self.timeLabel.text = _model.create_time;
    
//    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:_model.image] placeholderImage:nil];
    
    if (_model.image.length > 0) {
        
        self.topImageView.hidden = NO;
        
        
        UIImage *image0 = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:_model.image];
        
        CGFloat screenW = [UIScreen mainScreen].bounds.size.width - 56;
        if (image0) {
            
            NSLog(@"有图片呀有图片");
            
            self.topImageView.image = image0;
            
            CGFloat imageViewH = screenW / image0.size.width * image0.size.height;
            // 重置放大缩小的比例
            self.imageViewH.constant = imageViewH;
            
        }else{
            NSLog(@"没图片呀没图片");
            
            
            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:_model.image] options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                
                
            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                
                if (image) {
                    
                    self.topImageView.image = image;
                    
                    CGFloat imageViewH2 = screenW / image.size.width * image.size.height;
                    // 重置放大缩小的比例
                    self.imageViewH.constant = imageViewH2;
                    
                    
                }else{
                    
                    self.topImageView.hidden = YES;

                    self.imageViewH.constant = 0;
                    
                    
                }
                
                
            }];
            
            
        }
        
    } else {
            self.topImageView.hidden = YES;
        
        self.imageViewH.constant =  0;
    }
    
    if (self.imageViewH.constant > 200) {
        self.imageViewH.constant = 200;
    }
    
 
    
    
    
}


- (IBAction)topBtnDidTap:(UIButton *)sender {
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
